<template>
  <view v-if="!canRead" class="reader__buy">
    <view :class="{ 'reader__buy-inner': true, 'iphonex': isIphoneX }">
      <button class="reader__buy-btn" v-for="btn in buttons" :key="btn.key" @click="handleBtnClick(btn.key)">
        {{ btn.name }}
        <view v-if="btn.desc" class="reader__buy-desc">{{ btn.desc }}</view>
      </button>
    </view>
    <!-- 签到弹窗 -->
    <AttendanceDialog :show.sync="showAttendance" @close="closeAttendance" />
  </view>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import AttendanceDialog from "@/components/attendance-dialog";

export default {
  props: {
    canRead: Boolean
  },

  components: {
    AttendanceDialog
  },

  data() {
    return {
      showAttendance: false
    };
  },

  computed: {
    ...mapGetters(["themeConfig", "isIphoneX"]),
    ...mapState({
      bookid: state => state.reader.bookid,
      chapterNum: state => state.reader.chapterNum,
      isAutoBuy: state => state.reader.isAutoBuy,
      goodPrice: state => state.reader.goodPrice,
      userAmount: state => state.reader.userAmount,
    }),

    buttons() {
      return [
        // #ifndef MP-WEIXIN
        {
          key: "use-money",
          name: "使用书币阅读",
          desc: `需要${this.goodPrice}书币，剩余${this.userAmount}书币`
        },
        {
          key: "to-be-member",
          name: "成为会员，免费阅读"
        },
        // #endif
        // #ifdef MP-WEIXIN
        {
          key: "attendance",
          name: "签到后继续阅读",
        },
        // #endif
        {
          key: "invite-user",
          name: "邀请好友‧免费"
        },
        {
          key: "cancel",
          name: "取消"
        }
      ];
    }
  },

  methods: {
    handleBtnClick(key) {
      switch (key) {
        case "attendance":
          this.showAttendance = true;
          break;
        case "use-money":
          this.handleUseMoney();
          break;
        case "cancel":
          this.$emit('cancel');
          break;
        case "invite-user":
          this.$emit('share');
          break;
        case "to-be-member":
          uni.showModal({
            title: "温馨提示",
            content: "请联系客服升级会员",
            confirmText: "确认",
            success: res => {
              if (res.confirm) {
                uni.navigateTo({ url: "/sub-packages/about-us/index" });
              }
            }
          });
          break;
        default:
          break;
      }
    },

    handleUseMoney() {
      this.$http({
        method: "POST",
        url: "/api/front/chapter/buy",
        data: {
          bookid: this.bookid,
          chapterNum: this.chapterNum
        },
        needAuth: true,
        skipToast: true
      }).then(res => {
        if (res.ok) {
          this.$emit("update:canRead", true);
        } else if (res.data && res.data.nomoney) {
          // 费用不足
          uni.showModal({
            title: "温馨提示",
            content: "书币不足，您可以通过分享、签到等方式获得书币。",
            confirmText: "前往签到",
            success: res => {
              if (res.confirm) {
                this.showAttendance = true;
              }
            }
          });
        } else {
          this.$toast(res.msg || '购买失败，请重新尝试');
        }
      });
    },

    // 关闭签到弹窗
    closeAttendance() {
      this.showAttendance = false;
    }
  }
};
</script>

<style lang="scss">
.reader {
  &__buy {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent;
    z-index: 980;
    text-align: center;

    &-inner {
      position: relative;
      width: 100%;
      margin-top: 200rpx;
      height: calc(100% - 200rpx);
      background: linear-gradient(
        to bottom,
        rgba(255, 255, 255, 0) 0%,
        rgba(255, 255, 255, 1) 30%,
        rgba(248, 247, 252, 1) 100%
      );
      display: flex;
      flex-flow: column nowrap;
      justify-content: flex-end;
      align-items: center;
      
      &.iphonex {
        padding-bottom: 34px;
      }
    }

    &-btn {
      width: 90%;
      height: auto;
      line-height: 1;
      border-radius: 10rpx;
      box-sizing: border-box;
      text-align: center;
      font-size: 36rpx;
      background: #dadada;
      margin-bottom: 40rpx;
      padding: 30rpx 0;

      &.cancel-btn {
        color: #f72528;
      }
    }

    &-desc {
      font-size: 24rpx !important;
      color: #666 !important;
      margin-top: 12rpx !important;
    }
  }
}
</style>
